<?php
namespace app\index\controller;

use app\common\controller\HomeBase;  // model
use think\Db;                       // db
use app\common\model\Photo as PhotoModel;
use app\common\model\Nav as NavModel;
use app\common\model\Advimage as AdvimageModel;
use app\common\model\Slide as SlideModel;
use app\common\model\Article as ArticleModel;


class Index extends HomeBase
{

    private $_photo_model;
    private $_nav_model;
    private $_advimage_model;
    private $_slide_model;
    private $_article_model;
    /*
     * 控制器初始化
     * */
    public function _initialize()
    {
        if(is_mobile()){
//            $this->success('手机访问','Index/index');
            $this->redirect('Phone/index/index');
        }
        $this->_article_model = new ArticleModel();
        $this->_advimage_model = new AdvimageModel();
        $this->_photo_model = new PhotoModel();
        $this->_nav_model = new NavModel();
        $this->_slide_model = new SlideModel();
        $nav = $this->_nav_model->name('nav')->select();
        $leftadv = $this->_advimage_model->where("cid = 2 and status = 0")->order("sort desc")->select();
        $rightadv = $this->_advimage_model->where("cid = 1 and status = 0")->order("sort desc")->select();
        $this->assign('rightadv',$rightadv);
        $this->assign('leftadv',$leftadv);
        $this->assign('nav',$nav);
    }

    /*
     * 相册列表
     * */
    public function index($limit = 20)
    {
        //获取轮播图数据
        $play_data = $this->_slide_model->showList();
        $this->assign('play_datas',$play_data);
//        var_dump($play_data);
//        exit;
        $where = 'status = 0';
        $order = 'create_time DESC';
                    // limit
        $re =  $this->_photo_model->showPages($limit,$where,$order);
//                var_dump($re);
        //        exit;
        $this->assign('results',$re);
        // 这里做图片显示首页, 解析上传的图片数据
        return view('index');
    }

    /*
     * 相册列表
     * */
    public function detail($id){
        if(!empty($id)){
            $bloglists = $this->_article_model->field("id,title")->order("create_time desc")->select();
            foreach($bloglists as $val){
                $val['title'] = mb_strlen($val['title'],'utf-8') < 13 ? $val['title'] : mb_substr($val['title'],0,12).'...';
            }
            // 获取数据
            $re = $this->_photo_model->showOneArticle($id);
//            var_dump($re);
            if(is_array($re) && !empty($re)){
                $images_array = $re['0'];
                //点击进入加阅读量
                $reading = $images_array['reading'] + 1;
                $this->_photo_model->where("id",$id)->setField("reading",$reading);
//            var_dump($images_array);
                $photo_str = $images_array['photo'];
//            $photo_array = serialize($photo_str);
                $photo_array = unserialize($photo_str);
                //数组总个数
                $rowcount = count($photo_array);
//               // 对数据进行分页处理 5 张一页显示
               // $this->assign('bloglists',$bloglists);
                $this->assign('rowcount',$rowcount);
                $this->assign('photos',$photo_array);
                $this->assign('results',$re);
                return view('detail');
            }else{
                $this->error('错误页面','Index/index');
            }

        }else{
//            $this->success('新增成功', 'Article/index');
            $this->error('错误页面','Index/index');
        }
    }


    public function nav($limit = 20){
        //获取轮播图数据
        $play_data = $this->_slide_model->showList();
        $this->assign('play_datas',$play_data);
//        var_dump($play_data);
//        exit;
        $cid = $this->request->param('cid');
        $where = "cid = {$cid} and status = 0";
        $order = ['create_time' => 'DESC'];
        $re  = $this->_photo_model->showPages($limit,$where,$order);

        $this->assign('results',$re);
        // 这里做图片显示首页, 解析上传的图片数据
        return view('index');
    }

//    public function leftadv(){
//        $advimage = $this->_advimage_model->where("cid = 2")->select();
//        $this->assign('advimage',$advimage);
//        return view('index');
//    }

    public function search($limit = 20){
        $play_data = $this->_slide_model->showList();
        $this->assign('play_datas',$play_data);
        $key = $this->request->param();
        if($key != null){
            if($key['keyword'] == null){
                unset($key['keyword']);
                if ($key['sort'] == 2){
                    $where = "status = 0";
                    $order = "reading DESC";
                    $re = $this->_photo_model->showPages($limit,$where,$order);
                }else{
                    $where = "status = 0";
                    $order = "create_time DESC";
                    $re = $this->_photo_model->showPages($limit,$where,$order);
                }
            }else{
                $where = "author like '%{$key['keyword']}%' and status = 0";
                $order = "create_time DESC";
                $re = $this->_photo_model->showPages($limit,$where,$order);
            }
        }

//        $where = "status = 0";
//        $re =  $this->_photo_model->showPages($limit,$where);
//                var_dump($re);
        //        exit;
        $this->assign('results',$re);
        return view('index');
    }

    public function type($limit = 20){
        $play_data = $this->_slide_model->showList();
        $this->assign('play_datas',$play_data);
        $result = $this->request->get();
        $type = $result['type'];
        $sort = $result['sort'];
        if($sort == 2){
            $where = "status = 0";
            $order = "reading {$type}";
            $re = $this->_photo_model->showPages($limit,$where,$order);
        }elseif($sort == 1){
            $where = "status = 0";
            $order = "create_time {$type}";
            $re = $this->_photo_model->showPages($limit,$where,$order);
        }
        $this->assign('results',$re);
        return view('index');
    }



}
